In [1]:
#Let's get some data
povertyDf = read.csv("./data/world_poverty_in_millions.csv")
In [2]:
#lets make the dates into years
getYear = function(x) { as.numeric(format(as.Date(x), '%Y')) }
povertyDf$Year = unlist(lapply(povertyDf$Date, getYear))
In [3]:
#order the data by date asc
povertyDf = povertyDf[order(povertyDf$Year, decreasing = FALSE),]
In [4]:
head(povertyDf, 5)
In [5]:
#resize the default plot size so we can actually read it on screen
options(repr.plot.width=8, repr.plot.height=4)
In [6]:
plot(povertyDf$Year, povertyDf$Value)
In [7]:
#A very basic barchart. Just the two data sets.
barplot(povertyDf$Value, names.arg = povertyDf$Year)
In [8]:
# adding in some labels
barplot(povertyDf$Value, names.arg = povertyDf$Year,
main = "World Poverty in Millions",
xlab = "Year", ylab = "People in Poverty (Millions)")
In [9]:
#lets make things a little prettier using par()
par(bg = "#EDFBFF", las = 1, col.lab = "#262626", col.axis = "#262626",
cex.axis = 0.9, cex.lab = 1.25, cex.main = 1.5)
In [10]:
#lets tweak the look a little bit more
barplot(povertyDf$Value, names.arg = povertyDf$Year,
main = "World Poverty in Millions",
xlab = "Year", ylab = "People in Poverty (Millions)",
ylim = c(0, 2000), col = "#00B01A")
In [11]:
#let's try a line chart instead | type l = line chart
plot(povertyDf$Year, povertyDf$Value, type = "l",
main = "World Poverty in Millions",
xlab = "Year", ylab = "People in Poverty (Millions)",
ylim = c(0, 2000), xlim = c(1980, 2013), col = "#00475E", lwd = 3)
In [12]:
# we can save plots too | Supported formats include: svg(), pdf(), jpeg(), png()...
png("data/Poverty_In_Millions.png", width = 800, height = 1000)
barplot(povertyDf$Value, names.arg = povertyDf$Year,
main = "World Poverty in Millions",
xlab = "Year", ylab = "People in Poverty (Millions)",
ylim = c(0, 2000), col = "#00B01A")
dev.off() #turns off the plot saving